package com.lxinet.jeesns.modules.wechat.utils;

import com.blade.kit.http.HttpRequest;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

/**
 * Created by Administrator on 2017/4/13.
 */
public class FileUtil {

    /**
     * 获取发送图片文件
     * @param cookie
     * @param msgid
     * @return
     * @throws IOException
     */
    public static String getSendBase64ByImgFile(String cookie,String msgid) throws IOException {
        String url = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?MsgID="+msgid+"&type=slave";
        final File output = new File("send.jpg");
        HttpRequest.post(url).contentType("application/json;charset=utf-8")
                .header("Cookie", cookie).receive(output);
        /**
         String res = request.body();
         FileOutputStream fop = null;
         fop = new FileOutputStream(output);
         // if file doesnt exists, then create it
         if (!output.exists()) {
         output.createNewFile();
         }
         // get the content in bytes
         byte[] contentInBytes = res.getBytes();
         fop.write(contentInBytes);
         fop.flush();
         fop.close();**/
        return encodeImgageToBase64(output);
    }

    /**
     * 获取头像文件
     * @param cookie
     * @param headImgUrl
     * @throws IOException
     */
    public static String getBase64ByImgFile(String cookie,String headImgUrl) throws IOException {
        String url = "https://wx.qq.com"+headImgUrl.substring(1,headImgUrl.length()-1);
        final File output = new File("avatar.jpg");
         HttpRequest.post(url).contentType("application/json;charset=utf-8")
                .header("Cookie", cookie).receive(output);
        /**
        String res = request.body();
        FileOutputStream fop = null;
        fop = new FileOutputStream(output);
        // if file doesnt exists, then create it
        if (!output.exists()) {
            output.createNewFile();
        }
        // get the content in bytes
        byte[] contentInBytes = res.getBytes();
        fop.write(contentInBytes);
        fop.flush();
        fop.close();**/
        return encodeImgageToBase64(output);
    }

    /**
     * // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
     * @param imageFile
     * @return
     */
    public static String encodeImgageToBase64(File imageFile) {
        String dataBase64Str="";
        if(imageFile!=null&&imageFile.length()>0) {
            ByteArrayOutputStream outputStream = null;
            try {
                BufferedImage bufferedImage = ImageIO.read(imageFile);
                outputStream = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage, "jpg", outputStream);
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            dataBase64Str="data:image/jpg;base64," + encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
        }
        return dataBase64Str;
    }
}
